package com.yaorange.jk.web.cargo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.Export;
import com.yaorange.jk.entity.Invoice;
import com.yaorange.jk.entity.PackingList;
import com.yaorange.jk.service.ExportService;
import com.yaorange.jk.service.InvoiceService;
import com.yaorange.jk.service.PackingListService;
import com.yaorange.jk.service.ShippingOrderService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller("invoiceAction")
@Scope("prototype")
public class InvoiceAction extends BaseAction implements ModelDriven<Invoice>  {

    @Autowired
    private InvoiceService invoiceService;
    @Autowired
    private PackingListService packingListService;
    @Autowired
    private ShippingOrderService shippingOrderService;
    @Autowired
    private ExportService exportService;

    private Pagination page=new Pagination();

    private List<Invoice> invoiceList;

    private Invoice model= new Invoice();

    public String list(){

    Pagination pagination =  invoiceService.findPage(page);
    pagination.setUrl("invoiceAction_list");
    ActionContext.getContext().getValueStack().push(pagination);
    return "tolist";
    }

    public String tocreate(){
    invoiceList = invoiceService.findAll();
    //        ActionContext.getContext().getValueStack().set("invoiceList",invoiceList);
    page = shippingOrderService.findPage(page,1d);
    page.setUrl("invoiceAction_tocreate");
    push(page);
    return "tocreate";
    }

    public String insert(){
        invoiceService.save(model);
        return list();
    }

    public String toupdate(){
    Invoice invoice = invoiceService.findById(model.getId());
    super.push(invoice);

    invoiceList = invoiceService.findAll();
    invoiceList.remove(invoice);
    return "toupdate";
    }

    public String update(){
    invoiceService.update(model);
    return list();
    }
    
    public String invoice() throws ParseException{
    	String[] ids = model.getId().split(", ");
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        for (String id:ids) {
        	Invoice invoice= invoiceService.findById(id);
	        invoiceService.invoice(invoice);
	        
	        PackingList packingList = packingListService.findById(id);
        	String[] exportIds=packingList.getExportIds().split(", ");
        	for (String string : exportIds) {
				
        		Export export=exportService.findById(string);
        		export.setState(4); //设置export的状态为4，发票
        		exportService.update(export);
			}
        	packingList.setInvoiceNo(invoice.getScNo());
        	packingList.setInvoiceDate(sdf.parse(invoice.getBlNo()));
        }
    	
    	return list();
    }
    
    
    
    public String cancel(){
    	
    	String[] ids = model.getId().split(", ");
        for (String id:ids) {
        	Invoice invoice= invoiceService.findById(id);
        	invoiceService.cancel(invoice);
        	PackingList packingList = packingListService.findById(id);
        	String[] exportIds=packingList.getExportIds().split(", ");
        	for (String string : exportIds) {
				
        		Export export=exportService.findById(string);
        		export.setState(3); //设置export的状态为重置为3，委托
        		exportService.update(export);
			}
        }
    	return list();
    }

    /**
    * @return
    */
    public String delete(){
    String[] ids = model.getId().split(", ");
    for (String id:ids) {
    invoiceService.deleteById(model.getId());
    }

    return list();
    }

    public String toview()
    {
    Invoice invoice = invoiceService.findById(model.getId());
    push(invoice);
    return "toview";
    }
    public void setPage(Pagination page) {
    this.page = page;
    }

    public List<Invoice> getInvoiceList() {
    return invoiceList;
    }

    @Override
    public Invoice getModel() {
    return model;
    }
}
